﻿Imports System.Data.SqlClient

Public Class frmArchivePayVouchers

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Me.ErrProv.Clear()
        If Me.CombBranch.SelectedIndex = -1 Then
            Me.ErrProv.SetError(Me.CombBranch, "Please fill in all details")
        Else
            Try
                Me.Cursor = Cursors.Default

                Dim cmd As New SqlCommand("Select Distinct PaperNo,DestSource From Transactions Where TransType=N'Pay' and Branch=N'" & _
                                          Me.CombBranch.SelectedItem & "' and TransDate>=N'" & Me.DTPFrom.Value.ToShortDateString & " 00:00:00' " & _
                                          "and TransDate<=N'" & Me.DTPTo.Value.ToShortDateString & " 23:59:59'", cnn)
                Dim Reader As SqlDataReader

                Me.ListVouchers.Items.Clear()
                Me.CrystalReportViewer1.ReportSource = Nothing

                cnn.Open()
                Reader = cmd.ExecuteReader
                While Reader.Read
                    Dim Item As New ListViewItem
                    Item.Text = Reader.Item("PaperNo")
                    Item.SubItems.Add(Reader.Item("DestSource"))

                    Me.ListVouchers.Items.Add(Item)
                End While
                cnn.Close()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub ListVouchers_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListVouchers.SelectedIndexChanged
        Me.CrystalReportViewer1.ReportSource = Nothing

        If Me.ListVouchers.SelectedItems.Count > 0 Then
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim dap As New SqlDataAdapter("Select * From Transactions Where Transtype=N'Pay' and PaperNo=" & CInt(Me.ListVouchers.SelectedItems(0).Text) & _
                                              " and Branch=N'" & Me.CombBranch.Text & "' and TotalIn<>0", cnn)
                Dim das As New DataSet

                dap.Fill(das, "Transactions")

                Dim rpt As New PayBill
                rpt.SetDataSource(das)
                Me.CrystalReportViewer1.ReportSource = rpt

                Me.ListVouchers.Focus()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Sub FillBranches()
        Try
            Me.Cursor = Cursors.WaitCursor

            Me.CombBranch.Items.Clear()
            Dim Branches As ArrayList = GetBranches()

            For Each Branch As String In Branches
                Me.CombBranch.Items.Add(Branch)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmPayVouchersArchive_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized

        FillBranches()
    End Sub
End Class